package com.example.hyw.szysteam.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.hyw.szysteam.R;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

/**
 * Created by HYW on 2018/7/12.
 */

public class TestFinishActivity extends AppCompatActivity {
    QMUITopBar mTopBar;
    TextView tvScore;
    QMUIRoundButton back;
    QMUIRoundButton review;
    Bundle bundle;
    int score;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            tvScore.setText(String.valueOf(msg.arg1));
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testfinish);
        Bundle bundle=getIntent().getExtras();
        this.bundle=bundle;
        score=bundle.getInt("score");
        initScore(score);
        initView();
        initTopbar();
    }

    private void initView(){
        back=findViewById(R.id.go_back);
        review=findViewById(R.id.review);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        review.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(TestFinishActivity.this,MistakeBookActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });
    }
    private void initScore(final int score){
        tvScore=findViewById(R.id.score);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<=score;i++){
                    try{
                        Thread.sleep(20);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    Message msg=new Message();
                    msg.arg1=i;
                    mHandler.sendMessage(msg);
                }
            }
        }).start();
    }

    private  void initTopbar(){
        mTopBar=findViewById(R.id.topbar);
        mTopBar.setTitle("成绩").setTextColor(getResources().getColor(R.color.myWhite));
        mTopBar.addLeftImageButton(R.drawable.ic_left,R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
